package com.spider.spiderflowable.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author xiong.bo
 * @version 1.0
 * @date 2020/11/21 10:06
 */
public class DateUtils {

  public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  public static final ZoneId DEFAULT_ZONE_ID = ZoneId.of("Asia/Shanghai");

  public static Date getDate() {
    return new Date();
  }

  public static ZoneId defZoneId() {
    return DEFAULT_ZONE_ID;
  }

  public static String utc() {
    return "UTC" + defZoneId().getRules().getOffset(java.time.Instant.now()).getId()
        .replace("0", "")
        .replace(":", "");
  }

  /**
   * 获取当前时间，Timestamp格式的
   *
   * @return Timestamp
   */
  public static Timestamp getCurrentTimestamp() {
    long time = System.currentTimeMillis();
    return new Timestamp(time);
  }


  public static Date addSeconds(Date currentDate, int seconds) {
    return org.apache.commons.lang3.time.DateUtils.addSeconds(currentDate, seconds);
  }

  public static Date addDays(Date currentDate, int days) {
    return org.apache.commons.lang3.time.DateUtils.addDays(currentDate, days);
  }

  public static String format(Long timestamp) {
    SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
    format.setTimeZone(TimeZone.getTimeZone(DEFAULT_ZONE_ID));
    return format.format(new Date(timestamp));
  }

  public static String format(Timestamp timestamp) {
    if (timestamp == null) {
      return "";
    }
    return format(timestamp.getTime());
  }
}
